#include "Selector.h"
#include "Input/InputManager.h"

Selector::Selector(int startPosX, int startPosY)
	: Object()
	, m_SpriteSheetComp(nullptr)
	, m_CurrentPosWidth(startPosX)
	, m_CurrentPosHeight(startPosY)
	, m_TimePressed(0)
	, m_TimeToTriggerMove(0.5)
	, m_TimeMove(0)
	, m_TimeBetweenMoves(0.1)
{

}


Selector::~Selector()
{
}

void Selector::Initialize()
{
	m_SpriteSheetComp = new star::SpritesheetComponent(_T("Player/Selector.png"),_T("Selector"),_T("Selector"), true);
	AddComponent(m_SpriteSheetComp);
	GetTransform()->Translate(-5,-1);
}

void Selector::Reset()
{
	GetTransform()->Translate(-5,-1);
	m_CurrentPosHeight = 0;
	m_CurrentPosWidth = 0;
}

void Selector::Update(const star::Context& context)
{
#ifdef _WIN32
#pragma region Move 1 block
	//move 1 block
	if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_RIGHT) &&
		!star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_RIGHT, true))
	{
		m_TimePressed = 0;
		if(m_CurrentPosWidth < m_MaxHorizontalTiles - 2)
		{
			GetTransform()->Move(10,0);
			++m_CurrentPosWidth;
		}
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_LEFT) &&
		!star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_LEFT, true))
	{
		m_TimePressed = 0;
		if(m_CurrentPosWidth > 0)
		{
			GetTransform()->Move(-10,0);
			--m_CurrentPosWidth;
		}
		
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_UP) &&
		!star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_UP, true))
	{
		m_TimePressed = 0;
		if(m_CurrentPosHeight < m_MaxVerticalTiles - 1)
		{
			GetTransform()->Move(0,10);
			++m_CurrentPosHeight;
		}
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_DOWN) &&
		!star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_DOWN, true))
	{
		m_TimePressed = 0;
		if(m_CurrentPosHeight > 0)
		{
			GetTransform()->Move(0,-10);
			--m_CurrentPosHeight;
		}
	}
#pragma endregion

#pragma region Multiple blocks
	if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_RIGHT))
	{
		m_TimePressed += context.mTimeManager->GetSeconds();
		if(m_TimePressed >= m_TimeToTriggerMove)
		{
			m_TimeMove += context.mTimeManager->GetSeconds();
			if(m_TimeMove >= m_TimeBetweenMoves)
			{
				m_TimeMove -= m_TimeBetweenMoves;
				if(m_CurrentPosWidth < m_MaxHorizontalTiles - 2)
				{
					GetTransform()->Move(10,0);
					++m_CurrentPosWidth;
				}
			}
		}
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_LEFT))
	{
		m_TimePressed += context.mTimeManager->GetSeconds();
		if(m_TimePressed >= m_TimeToTriggerMove)
		{
			m_TimeMove += context.mTimeManager->GetSeconds();
			if(m_TimeMove >= m_TimeBetweenMoves)
			{
				m_TimeMove -= m_TimeBetweenMoves;
				if(m_CurrentPosWidth > 0)
				{
					GetTransform()->Move(-10,0);
					--m_CurrentPosWidth;
				}
			}
		}
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_UP))
	{
		m_TimePressed += context.mTimeManager->GetSeconds();
		if(m_TimePressed >= m_TimeToTriggerMove)
		{
			m_TimeMove += context.mTimeManager->GetSeconds();
			if(m_TimeMove >= m_TimeBetweenMoves)
			{
				m_TimeMove -= m_TimeBetweenMoves;
				if(m_CurrentPosHeight < m_MaxVerticalTiles - 1)
				{
					GetTransform()->Move(0,10);
					++m_CurrentPosHeight;
				}
			}
		}
	}
	else if(star::InputManager::GetInstance()->IsKeyboardKeyDown(VK_DOWN))
	{
		m_TimePressed += context.mTimeManager->GetSeconds();
		if(m_TimePressed >= m_TimeToTriggerMove)
		{
			m_TimeMove += context.mTimeManager->GetSeconds();
			if(m_TimeMove >= m_TimeBetweenMoves)
			{
				m_TimeMove -= m_TimeBetweenMoves;
				if(m_CurrentPosHeight > 0)
				{
					GetTransform()->Move(0,-10);
					--m_CurrentPosHeight;
				}
			}
		}
	}
#pragma endregion
#endif
}

void Selector::GetCurrentPos(int& posX, int& posY)
{
	posX = m_CurrentPosWidth;
	posY = m_CurrentPosHeight;
}

void Selector::SetMaxHorizontalTiles(int32 nr)
{
	m_MaxHorizontalTiles = nr;
}

void Selector::SetMaxVerticalTiles(int32 nr)
{
	m_MaxVerticalTiles = nr;
}
